package com.study.mybatis.test;

import com.study.mybatis.bean.Person;
import com.study.mybatis.mapper.PersonMapper;
import com.study.mybatis.mapper.PersonMsgMapper;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.binding.MapperProxyFactory;
import org.apache.ibatis.binding.MapperRegistry;
import org.apache.ibatis.builder.StaticSqlSource;
import org.apache.ibatis.builder.xml.XMLConfigBuilder;
import org.apache.ibatis.executor.SimpleExecutor;
import org.apache.ibatis.io.Resources;
import org.apache.ibatis.mapping.BoundSql;
import org.apache.ibatis.mapping.MappedStatement;
import org.apache.ibatis.reflection.MetaObject;
import org.apache.ibatis.scripting.defaults.DefaultParameterHandler;
import org.apache.ibatis.scripting.defaults.RawSqlSource;
import org.apache.ibatis.session.Configuration;
import org.apache.ibatis.session.SqlSession;
import org.apache.ibatis.session.SqlSessionFactory;
import org.apache.ibatis.session.SqlSessionFactoryBuilder;
import org.apache.ibatis.session.defaults.DefaultSqlSession;
import org.apache.ibatis.session.defaults.DefaultSqlSessionFactory;
import org.apache.ibatis.type.TypeHandlerRegistry;

import java.io.IOException;

public class Basic {
    public static void main(String[] args) throws Exception {
        //1、构建SqlSessionFactory
        SqlSessionFactory sqlSessionFactory = new SqlSessionFactoryBuilder().build(Resources.getResourceAsStream("mybatis-config.xml"));
        //2、创建SqlSession  主要功能：2.1、获取映射器，2.2、通过命名信息去执行sql返回结果。
        SqlSession sqlSession = sqlSessionFactory.openSession();
        //3、创建映射器
        PersonMapper personMapper = sqlSession.getMapper(PersonMapper.class);
        //4、执行查询
        personMapper.selectAll(false, false);
        //测试是否开启一级缓存。断点：PreparedStatementHandler.query(Statement statement, ResultHandler resultHandler) throws SQLException
        personMapper.selectAll(false, false);
        //测试是否开启二级缓存。断点：PreparedStatementHandler.query(Statement statement, ResultHandler resultHandler) throws SQLException
        sqlSessionFactory.openSession().getMapper(PersonMapper.class).selectAll(false, false);

        //5、通过sqlSession执行查询
        //sqlSession.selectList("com.study.mybatis.mapper.PersonMapper.selectAll", false);
        //6、通过sqlSession执行插入操作
        Person person = new Person();
        person.setPersonName("王小翠");
        //personMapper.insertOne(person);
        sqlSession.commit(true);
        //7、核心类：MappedStatement、StaticSqlSource、BoundSql、
        // DefaultSqlSessionFactory、DefaultSqlSession、SimpleExecutor、
        // MapperProxyFactory、DefaultParameterHandler、、、、
        MappedStatement mappedStatement;
        StaticSqlSource staticSqlSource;
        BoundSql boundSql;
        DefaultSqlSessionFactory sqlSessionFactory1;
        MapperProxyFactory factory;
        DefaultSqlSession defaultSqlSession;
        SimpleExecutor simpleExecutor;
        DefaultParameterHandler parameterHandler;
        XMLConfigBuilder xmlConfigBuilder;
        MetaObject metaObject;

    }
}